package com.example.cp.service;

import com.example.cp.config.WxCpConfiguration;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by hemin on 2019/8/9.
 */
@Service
public class SendMsgServiceImpl implements SendMsgService{


    RestTemplate restTemplate = new RestTemplate();

    /**
     * 向指定用户发送消息
     * @param agentId 应用Id
     * @param userIds UserID列表
     * @param content 内容
     */
    public WxCpMessageSendResult text(Integer agentId, String userIds, String content) throws WxErrorException {
        WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
        WxCpMessage wxCpMessage =
                WxCpMessage.TEXT()
                            .agentId(agentId)
                            .toUser(userIds)
                            .content(content)
                            .build();
        return wxCpService.messageSend(wxCpMessage);
    }

    /**
     * 向指定用户发送卡片消息
     * @param agentId 应用Id
     * @param userIds UserID列表
     */
    /*public WxCpMessageSendResult textCard(String title,String description,String url,Integer agentId, String userIds) throws WxErrorException {
        WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        WxCpMessage wxCpMessage =
                WxCpMessage.TEXTCARD()
                                .agentId(agentId)
                                .toUser(userIds)
                                .title(title)
                                .description(description)
                                .url(url)
                                .build();
        String sendUrl = WxCpService.MESSAGE_SEND;
        String json = wxCpMessage.toJson();
        String ACCESS_TOKEN = wxCpService.getAccessToken();
        HttpEntity<String> entity = new HttpEntity<>(json, headers);
        ResponseEntity<WxCpMessageSendResult> wxCpMessageSendResult = restTemplate.postForEntity(sendUrl+"?access_token="+ACCESS_TOKEN, entity, WxCpMessageSendResult.class);
        return wxCpMessageSendResult.getBody();
        //return wxCpService.messageSend(wxCpMessage);
    }*/

    /**
     * 向指定用户发送卡片消息
     * @param agentId 应用Id
     * @param userIds UserID列表
     */
    public WxCpMessageSendResult textCard(String title,String description,String url,Integer agentId, String userIds,String btntxt) throws WxErrorException {
        WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
        WxCpMessage wxCpMessage =
                WxCpMessage.TEXTCARD()
                        .agentId(agentId)
                        .toUser(userIds)
                        .title(title)
                        .description(description)
                        .url(url)
                        .btnTxt(btntxt)
                        .build();
        return wxCpService.messageSend(wxCpMessage);
    }
}
